package com.ysd.lis.controller.sys;


import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.common.Result;
import com.ysd.common.dto.OrganizationDTO;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.request.*;
import com.ysd.lis.service.SysUserService;
import com.ysd.util.PropertySlash;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * cold_chain.t_user 前端控制器
 * </p>
 *
 * @author peng
 * @since 2021-10-08
 */
@Api(tags = "用户信息")
@RequestMapping("/api/user")
@RestController
public class SysUserController extends BaseController {
    @Value("${picturePath}")
    private String picturePath;
    private static final int MAX_POST_SIZE = 20 * 1024 * 1024;

    @Autowired
    SysUserService sysUserService;

    @ApiOperation("查询用户列表")
    @GetMapping("/findUserList")
//    @RequiresPermissions("system:user:list")
    public Result getList(userPageParam userPageParam) {
        return sysUserService.getlist(userPageParam);
    }

    @ApiOperation("根据组织机构查询员工列表")
    @GetMapping("/findAllUserListByOrgDept")
    public Result findAllUserListByOrgDept(userPageParam userPageParam) {
        return sysUserService.findAllUserListByOrgDept(userPageParam);
    }

    @ApiOperation("查询用户列表通过orgId")
    @GetMapping("/findUserListByOrgId")
    public Result findUserListByOrgId(userPageParam userPageParam) {
        return sysUserService.getlist(userPageParam);
    }


    @ApiOperation("根据ID查询单条信息")
    @PostMapping("/getUserById")
    public Result getUserById(@RequestBody SysUser t) {
        return sysUserService.getUserById(t);
    }

    @ApiOperation("查询社区服务中心机构")
    @PostMapping("/getOrganization")
    public Result getOrganization(@RequestBody SysUser t) {
        return sysUserService.getUserById(t);
    }


    @ApiOperation("根据ID删除单条信息")
    @PostMapping("/removeUserById")
    public Result removeUserById(@RequestBody SysUser t) {
        return sysUserService.removeUserById(t);
    }

    @ApiOperation("获取机构中心")
    @PostMapping("/api/sys/findOrgAllList")
    public Result getOrg() {
        return sysUserService.getOrganization();
    }

    @ApiOperation("获取角色信息")
    @PostMapping("/api/sys/findRoleList")
    public Result getRole() {
        return sysUserService.getRole();
    }

    @ApiOperation("根据机构获取部门")
    @GetMapping("/api/sys/findDepartmentListByOrgId")
    public Result getDepartByOrg(OrgDepartmentParam orgDepartmentParam) {
        return sysUserService.getOrgDepartment(orgDepartmentParam);
    }

    @ApiOperation("新增用户")
    @PostMapping("api/user/addUser")
    public Result addUser(@RequestBody userAddParam user) {
        return sysUserService.addUser(user);
    }

    @ApiOperation("新增用户")
    @PostMapping("/addUserInfo")
    @LogDetail(requestParam = "addUser", operationType = OperationType.INSERT, module = "用户管理", content = "新增用户【username】操作", businessName = "username")
    public Result addUserInfo(@RequestBody userAddParam user) {
        return sysUserService.addUserInfo(user);
    }

    @ApiOperation("根据ID获取用户单条信息")
    @GetMapping("/findUserSimple")
    public Result findUserSimple(@RequestParam String id) {
        return sysUserService.findUserSimple(id);
    }

    @ApiOperation("修改用户")
    @PostMapping("/editUser")
    public Result editUser(@RequestBody userAddParam user) {
        return sysUserService.eiditUser(user);
    }

    @ApiOperation("仅修改用户信息")
    @PostMapping("/onlyEditUser")
    public Result onlyEditUser(@RequestBody SysUser user) {
        if (StringUtils.isBlank(user.getId())) {
            return Result.fail("查无用户信息");
        }
        return sysUserService.saveOrUpdate(user) ? Result.succ(1, "修改成功", user) : Result.fail("修改失败");
    }

    @ApiOperation("修改用户")
    @PostMapping("/editUserInfo")
    @LogDetail(requestParam = "editUserInfo", operationType = OperationType.UPDATE, module = "用户管理", content = "修改用户【username】操作", businessName = "username")
    public Result editUserInfo(@RequestBody userAddParam user) {
        return sysUserService.editUserInfo(user);
    }

    @ApiOperation("删除用户")
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestBody userDeleteParam user) {
        return sysUserService.userDeleteById(user);
    }

    @ApiOperation("根据用户id删除用户")
    @PostMapping("/deleteUserById")
    @LogDetail(requestParam = "deleteUserById", operationType = OperationType.DELETE, module = "用户管理", content = "删除用户【username】信息", businessName = "username")
    public Result deleteUserById(@RequestBody userAddParam user) {
        return sysUserService.deleteUserById(user.getId());
    }

    @ApiOperation("获取用户菜单权限")
    @GetMapping("/findUserRoleMenuPermissionList")
    public Result findUserRoleMenuPermissionList(@RequestParam String userId, String defalMenuId) {
        return sysUserService.findUserRoleMenuPermissionList(userId, defalMenuId);
    }

    @ApiOperation("获取用户菜单权限")
    @GetMapping("/findUserRoleMenuPermissionList1")
    public Result findUserRoleMenuPermissionList(@RequestParam String userId) {
        return sysUserService.findUserRoleMenuPermissionList(userId);
    }

    @ApiOperation("修改密码")
    @PostMapping("/changePassword")
    @LogDetail(requestParam = "changePassword", operationType = OperationType.UPDATE, module = "用户管理", content = "重置用户【username】密码操作", businessName = "username")
    public Result changePassword(@RequestBody changePasswordParam changePassword) {
        return sysUserService.changePassword(changePassword);
    }

    @ApiOperation("根据部门获取人员信息")
    @GetMapping("/getPersionByDeptId")
    public Result getPersionByDeptId(String deptId, Integer pageIndex, Integer pageSize) {
        return sysUserService.getPersionByDeptId(deptId, pageSize, pageIndex);
    }

    @ApiOperation("获取人员信息（不分页)")
    @GetMapping("/getUserInfoList")
    public Result getUserInfoList(SysUser user) {
        return sysUserService.getUserInfoList(user);
    }

    @ApiOperation("获取检验人员信息（不分页)")
    @GetMapping("/getTestUserInfoList")
    public Result getTestUserInfoList(SysUser user) {
        return sysUserService.getTestUserInfoList(user);
    }

    @ApiOperation("获取审核人员信息（不分页)")
    @GetMapping("/getChkUserInfoList")
    public Result getChkUserInfoList(SysUser user) {
        return sysUserService.getChkUserInfoList(user);
    }

    @ApiOperation("初始化")
    @PostMapping("/initializeUser")
    public Result initializeUser(@RequestBody SysUser user) {
        boolean b = sysUserService.updateById(user);
        if (b) {
            return Result.succ(1, "初始化成功", "");
        }
        return Result.fail("初始化失败");
    }

    @ApiOperation("分页查询")
    @GetMapping("/getUserPageList")
    public Result getUserPageList(userPageParam pageParam) {
        return sysUserService.getUserPageList(pageParam);
    }

    @ApiOperation("导入员工数据")
    @PostMapping("/importUserByEmploy")
    @LogDetail(requestParam = "importUserByEmploy", operationType = OperationType.IMPORT, module = "用户管理", content = "导入员工操作")
    public Result importUserByEmploy(@RequestBody ImportUserAddParam importUserAddParam) {
        return sysUserService.importUserByEmploy(importUserAddParam);
    }

    @ApiOperation("验证授权码")
    @GetMapping("/checkEmpowerNum")
    public Result checkEmpowerNum(@RequestParam("username") String username, @RequestParam("empowerNum") String empowerNum) {
        return sysUserService.checkEmpowerNum(username, empowerNum);
    }

    @ApiOperation("获取电子签名连接")
    @GetMapping("/getSignatureFile/{id}")
    public Result getSignatureFile(@PathVariable("id") String id) throws IOException {
        return sysUserService.getSignatureFilePath(id);
    }

    @ApiOperation("上传电子签名")
    @PostMapping("/uploadSignatureFile")
    @LogDetail(operationType = OperationType.UPDATE, module = "上传电子签名")
    public Result uploadSignatureFile(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        String id = request.getParameter("id");
        if (file == null) {
            return Result.fail("文件不能为空");
        }
        // 文件限制10M
        long size = file.getSize();
        if (size > MAX_POST_SIZE) {
            return Result.fail("文件最大为 10M！");
        }
        String folder = picturePath + PropertySlash.getSlash();
        if (!FileUtil.exist(folder)) {
            FileUtil.mkdir(folder);
        }
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS")) + file.getOriginalFilename();
        String path = folder + fileName;
        File files = new File(path);
        if (FileUtil.exist(files)) {
            return Result.fail("文件已存在");
        }
        File file1 = FileUtil.writeBytes(file.getBytes(), path);
        if (file1.length() < 0) {
            return Result.fail("文件上传失败");
        }
        return sysUserService.uploadSignatureFile(id, path);
    }

    @ApiOperation("测试")
    @PostMapping("/test")
    public Result syncUserInfo(@RequestBody OrganizationDTO organizationDTO) {
        return sysUserService.syncUserInfo(organizationDTO);
    }

}
